package com.jd.finance.core.controller.admin;

import com.jd.finance.core.service.ProductService;
import com.jd.finance.core.service.RateService;
import com.jd.finance.core.service.UserService;
import com.jd.finance.core.service.impl.AdminUtilsServiceImpl;
import com.jd.finance.result.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

@Slf4j
@Api(tags = "添加产品页")
@RestController
@RequestMapping("admin/core/adminLoanOfficerCenter")
public class AdminAddProductController {

    @Resource
    private AdminUtilsServiceImpl adminUtilsService;
    @Resource
    private ProductService productService;
    @Resource
    private UserService userService;
    @Resource
    private RateService rateService;

    @ApiOperation("根据银行信贷员uid，查询所属银行信息")
    @GetMapping("getBelongBankInfo")
    public BaseResult getBelongBankInfo(Integer adminId, Integer uid) {
        if (uid == null || adminId == null) {
            log.error("传入参数为空C");
            return BaseResult.error().message("传入参数为空");
        }
        if (!adminUtilsService.isAdmin(adminId)) {
            log.error("权限不足C");
            return BaseResult.error().message("权限不足C");
        }
        if (!adminUtilsService.isBank(uid)) {
            log.error("该用户不是银行信贷员");
            return BaseResult.error().message("该用户不是银行信贷员");
        }

        return userService.getBelongBankInfo(uid);


    }


    @ApiOperation("添加贷款产品")
    @PostMapping("/addLoanProduct")
    public BaseResult addLoanProduct(Integer uid, BigDecimal rate, BigDecimal money, String proName,
                                     String proInfo, String proKey, String repayWay, HttpServletRequest request) {

        if (uid == null || rate == null || money == null) {
            log.error("参数为空C");
            return BaseResult.error().message("参数为空");
        }
        if (StringUtils.isAllBlank(proName, proInfo, proInfo, proKey,repayWay)) {
            log.error("参数为空C");
            return BaseResult.error().message("参数为空");
        }
        if (!adminUtilsService.isBank(uid)) {
            log.error("无访问权限C");
            return BaseResult.error().message("无访问权限");
        }

        return rateService.addLoanProduct(uid, rate, money, proName, proInfo, proKey, repayWay, request);

    }

    @ApiOperation("根据产品名称模糊查询，查询产品说明，与不同银行的基本利率")
    @GetMapping("/getLoanProductInfo")
    public BaseResult getLoanProductInfo(Integer uid, String proName){
        if (uid == null){
            log.error("传入参数为空C");
            return BaseResult.error().message("传入参数为空");
        }
        if (StringUtils.isBlank(proName)){
            log.error("传入参数为空C");
            return BaseResult.error().message("传入参数为空");
        }
        if (!adminUtilsService.isBank(uid)){
            log.error("无访问权限");
            return BaseResult.error().message("无访问权限");
        }

        return productService.getLoanProductInfo(uid, proName);



    }


}
